package cate.game.pvp.teampvp;

import cate.common.util.GameResult;
import cate.game.framework.ModuleMan;
import cate.game.play.part.PlaySide;
import cate.game.pvp.teampvp.dto.*;
import cate.game.rank.po.Rank;
import cate.game.role.Role;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@SuppressWarnings("unchecked")
public class TeamPvpMsgManager extends ModuleMan {
    @Override
    public void initDependent() throws Exception {

    }

    public SynTeamPvpTeamData syncTeamData(PlaySide ps) {
        try{
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.SYNC_TEAM_DATA,
                    TeamPvpReqParams.SyncTeamDataReq.of(ps, game.config.gs.getAbbr()),
                    SynTeamPvpTeamData.class);
        } catch (Exception e) {
            log.error("组队竞技 SYNC_TEAM_DATA 失败，e=", e);
        }
        return null;
    }

    public TeamPvpPlayersData inviteList(Role role) {
        try{
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.INVITE_LIST,
                    TeamPvpReqParams.TeamPvpInviteListReq.of(role.getUid()),
                    TeamPvpPlayersData.class);
        } catch (Exception e) {
            log.error("组队竞技 INVITE_LIST 失败，e=", e);
        }
        return null;
    }

    public GameResult inviteApply(Role role, String playerUid) {
        try{
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.INVITE_APPLY,
                    TeamPvpReqParams.TeamPvpInviteApplyReq.of(role.getUid(), playerUid),
                    GameResult.class
            );
        } catch (Exception e) {
            log.error("组队竞技 INVITE_APPLY 失败，e=", e);
        }
        return new GameResult().fail("邀请失败拉");
    }

    public GameResult<TeamPvpTeamData> inviteAnswer(Role role, String leaderUid, boolean ok) {
        try {
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.INVITE_ANSWER,
                    TeamPvpReqParams.TeamPvpInviteAnswerReq.of(leaderUid, role.getUid(), ok),
                    GameResult.class
            );
        } catch (Exception e) {
            log.error("组队竞技 INVITE_ANSWER 失败，e=", e);
        }
        return new GameResult<TeamPvpTeamData>().fail("操作失败拉");
    }

    public GameResult<TeamPvpMatchData> match(Role role) {
        try {
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.BEGIN_TO_MATCH,
                    role.getUid(),
                    GameResult.class
            );
        } catch (Exception e) {
            log.error("组队竞技开始匹配失败，e=", e);
        }
        return new GameResult<TeamPvpMatchData>().fail("匹配失败拉");
    }

    public GameResult<TeamPvpMatchData> matchRefresh(Role role) {
        try {
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.MATCH_REFRESH,
                    role.getUid(),
                    GameResult.class
            );
        } catch (Exception e) {
            log.error("组队竞技 MATCH_REFRESH 失败，e=", e);
        }
        return new GameResult<TeamPvpMatchData>().fail("刷新失败拉");
    }

    public void beginToChallenge(Role role) {
        try {
            game.rpcSend().fireAndForgetGroup(
                    TeamPvpMsg.BEGIN_TO_CHALLENGE,
                    role.getUid()
            );
        } catch (Exception e) {
            log.error("组队竞技 BEGIN_TO_CHALLENGE 失败，e=", e);
        }
    }

    public TeamPvpMatchData aheadPosition(String roleUid) {
        try {
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.AHEAD_TO_POSITION,
                    roleUid,
                    TeamPvpMatchData.class
            );
        } catch (Exception e) {
            log.error("组队竞技 AHEAD_TO_POSITION 失败，e=", e);
        }
        return null;
    }

    public TeamPvpPlayerSimpleData searchPlayer(String name) {
        try {
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.SEARCH_NAME,
                    name,
                    TeamPvpPlayerSimpleData.class
            );
        } catch (Exception e) {
            log.error("组队竞技 SEARCH_NAME 失败，e=", e);
        }
        return null;
    }

    public GameResult<TeamPvpMatchData> updateFightPosition(Role role, List<String> uids) {
        try {
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.UPDATE_FIGHT_POS,
                    TeamPvpReqParams.UpdateFightPosReq.of(role.getUid(), new ArrayList<>(uids)),
                    GameResult.class
            );
        } catch (Exception e) {
            log.error("组队竞技 UPDATE_FIGHT_POS 失败，e=", e);
        }
        return new GameResult<TeamPvpMatchData>().fail("失败拉");
    }

    public TeamPvpMatchData savePosition(PlaySide ps) {
        try {
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.SAVE_POSITION,
                    TeamPvpReqParams.SavePositionReq.of(ps, game.config.gs.getAbbr()),
                    TeamPvpMatchData.class
            );
        } catch (Exception e) {
            log.error("组队竞技 SAVE_POSITION 失败，e=", e);
        }
        return null;
    }

    public List<Rank> loadRanks() {
        try {
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.GET_RANK_LIST,
                    null,
                    ArrayList.class
            );
        } catch (Exception e) {
            log.error("组队竞技 GET_RANK_LIST 失败，e=", e);
        }
        return Collections.emptyList();
    }

    public List<Rank> loadGods() {
        try {
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.GET_GOD_LIST,
                    null,
                    ArrayList.class
            );
        } catch (Exception e) {
            log.error("组队竞技 GET_GOD_LIST 失败，e=", e);
        }
        return Collections.emptyList();
    }

    public TeamPvpInviteApplyData loadInviteApplyList(String roleUid) {
        try {
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.INVITE_APPLY_LIST,
                    roleUid,
                    TeamPvpInviteApplyData.class
            );
        } catch (Exception e) {
            log.error("组队竞技 INVITE_APPLY_LIST 失败，e=", e);
        }
        return null;
    }

    public GameResult<TeamPvpTeamData> changeLeader(String leaderUid, String newLeaderUid) {
        try {
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.CHANGE_LEADER,
                    TeamPvpReqParams.ChangeLeaderReq.of(leaderUid, newLeaderUid),
                    GameResult.class
            );
        } catch (Exception e) {
            log.error("组队竞技 CHANGE_LEADER 失败，e=", e);
        }
        return new GameResult<TeamPvpTeamData>().fail("失败拉");
    }

    public GameResult<TeamPvpTeamData> exitTeam(String playerUid) {
        try {
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.EXIT_TEAM,
                    TeamPvpReqParams.ExitTeamReq.of(playerUid),
                    GameResult.class
            );
        } catch (Exception e) {
            log.error("组队竞技 EXIT_TEAM 失败，e=", e);
        }
        return new GameResult<TeamPvpTeamData>().fail("失败拉");
    }

    public GameResult<Void> gmResetChallengeTimes(String playerUid) {
        try {
            return game.rpcSend().requestResponseGroup(
                    TeamPvpMsg.GM_RESET_CHALLENGE_TIMESE,
                    playerUid,
                    GameResult.class
            );
        } catch (Exception e) {
            log.error("组队竞技 GM_RESET_CHALLENGE_TIMESE 失败，e=", e);
        }
        return new GameResult<Void>().fail("失败拉");
    }
}
